Repository: sveltejs/svelte (JavaScript) — Feature

Issue:
Any way to read the value of a writable?
In my store.js, I may have:

`const player = writable({name: 'Player A', highScore: 50});
`
I may need a function in the store file:

```
const tick = (latestScore) => {
    const highScore = player. highScore;
    if (latestScore > highScore) {
       //set the player's highScore...
    }
}
```

The idea would be that e.g main.js could invoke 'tick' whenever it needed to and pass in a score, which would conditionally update based on a read.

A delightfully simple task...until the the "reading" of the player's highScore. Obvs, binding this value in a visual component works fine. But, short of using OCR to watch my screen, I have not discovered how to extract data from a writable object in code :-)



Discussion:
AFAIK you'd use `player.subscribe()` and pass it a callback to get notified when the value changed. You could then save that off in a closure variable somewhere to reference from your `tick` function.
Aaah - ok. Thanks. I did briefly look at that possibility, and was immediately reminded of John McEnroe remonstrating with an umpire :-) 

Writable stores also have a method `update` which can be passed a function that takes the current value of the store and returns what the store's value should be updated to. https://github.com/sveltejs/rfcs/blob/master/text/0002-reactive-stores.md#store-api
 Yes, I see how (for UI components) this is exactly how you'd want to go. Most observer implementations work along similar lines; but they usually leave a getter in place for components that are *not* event-driven to be able to inspect state on an ad-hoc basis. Anyway, I'll see what breaks if I extend the writable and add my own getter.
Something like this is what I meant:

```javascript
const tick = (latestScore) => {
  player.update(({ name, highScore }) => {
    if (highScore < latestScore) highScore = latestScore;
    return { name, highScore };
  });
}
```

If you do need to get the current value in a component definition at any moment, and `player` is in the top level of your component scope, then you can use `$player`. Even in methods in your script tag, this will be the current value of the store.
Thanks. Inside script tags I've found no issues it's nice and easy to get the value the value. What goes on inside teh script blocks is strong and makes Svelte v cool. I have several parts of an app where the logic is not in script tags but in separate files. In those cases needing to 'update' objects before we can get their value is an interesting design decision.
This would not work with `readable` or `derived`. In that case, `subscribe` work for `readable`, `derived`, and `writable`.

```javascript
function get__store(store) {
  let $val
  store.subscribe($ => $val = $)()
  return $val
}
```

Resolving patch:
diff --git a/store.mjs b/store.mjs
--- a/store.mjs
+++ b/store.mjs
@@ -1,4 +1,4 @@
-import { run_all, noop } from './internal';
+import { run_all, noop, get_store_value } from './internal';
 
 export function readable(start, value) {
 	const subscribers = [];
@@ -103,3 +103,5 @@ export function derive(stores, fn) {
 		};
 	});
 }
+
+export { get_store_value as get };


Test patch:
diff --git a/test/store/index.js b/test/store/index.js
--- a/test/store/index.js
+++ b/test/store/index.js
@@ -1,5 +1,5 @@
 import * as assert from 'assert';
-import { readable, writable, derive } from '../../store.js';
+import { readable, writable, derive, get } from '../../store.js';
 
 describe('store', () => {
 	describe('writable', () => {
@@ -172,4 +172,11 @@ describe('store', () => {
 			unsubscribe();
 		});
 	});
+
+	describe('get', () => {
+		it('gets the current value of a store', () => {
+			const store = readable(() => {}, 42);
+			assert.equal(get(store), 42);
+		});
+	});
 });