-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { writable, type Invalidator, type Subscriber, type Writable } from 'svelte/store'; | ||
|
||
type Options = { | ||
allowedOrigins?: string[]; | ||
id?: string; | ||
onChange?: (value: any) => void; | ||
}; | ||
|
||
/** | ||
* Creates a writable Svelte store. | ||
* @param initialValue | ||
* @param options | ||
* @returns | ||
* @example | ||
* ```ts | ||
* createStore({ | ||
* allowedOrigins = ['*'], | ||
* id = 'svelte-crossorigin-store:message', | ||
* onChange = undefined, | ||
* }); | ||
*``` | ||
*/ | ||
export function createWritableStore<T>(initialValue: T, { | ||
allowedOrigins = ['*'], | ||
id = 'svelte-crossorigin-store:message', | ||
onChange = undefined, | ||
}: Options = {}): Writable<T> { | ||
const store = writable<T>(initialValue); | ||
const { subscribe, set, update } = store; | ||
|
||
const onMessage = (event: MessageEvent) => { | ||
if ((allowedOrigins.includes(event.origin) || allowedOrigins.includes('*')) && event.data.id === id) { | ||
set(event.data.value); | ||
} | ||
}; | ||
|
||
const _sharedSubscribe = (run: Subscriber<any>, invalidate: Invalidator<any> = () => { }) => { | ||
subscribe(run, invalidate); | ||
|
||
window.addEventListener('message', onMessage); | ||
|
||
return () => { | ||
window.removeEventListener('message', onMessage); | ||
}; | ||
}; | ||
|
||
const _postMessageToManyOrigins = (target: Window, value: any) => { | ||
allowedOrigins.forEach(origin => { | ||
target?.postMessage({ id, value }, origin); | ||
}); | ||
} | ||
|
||
store.subscribe(value => { | ||
_postMessageToManyOrigins(window, value); | ||
|
||
if (typeof onChange === 'function') { | ||
onChange(value); | ||
} | ||
}); | ||
|
||
return { | ||
subscribe: _sharedSubscribe, | ||
set, | ||
update, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters