-
Notifications
You must be signed in to change notification settings - Fork 8
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
1 parent
5d10e72
commit 6c1f208
Showing
32 changed files
with
257 additions
and
168 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
import { StoryStore } from './dist/types'; | ||
export declare let store: StoryStore; | ||
import { Store } from '@component-controls/core'; | ||
export declare let store: Store; |
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 |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import { | ||
saveStore, | ||
notifyStoreReload, | ||
} from './serialization/store-local-storage'; | ||
import { loadStore } from './serialization/load-store'; | ||
import { BroadcastStore } from './serialization/BroadcastStore'; | ||
import { Store, defaultStore } from '@component-controls/core'; | ||
|
||
const bundle = require('@component-controls/loader/story-store-data.js'); | ||
|
||
/** | ||
* store variable, automatically filled with stories. | ||
*/ | ||
export const store = loadStore( | ||
require('@component-controls/loader/story-store-data.js'), | ||
export const store: Store = new BroadcastStore( | ||
bundle ? loadStore(bundle) : defaultStore, | ||
); | ||
saveStore(store); | ||
notifyStoreReload(); | ||
|
||
if (bundle) { | ||
(store as BroadcastStore).saveStore(); | ||
} |
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,94 @@ | ||
import { BroadcastChannel } from 'broadcast-channel'; | ||
import { | ||
Store, | ||
Story, | ||
StoreObserver, | ||
RunConfiguration, | ||
Documents, | ||
StoreStories, | ||
StoreComponents, | ||
StorePackages, | ||
} from '@component-controls/core'; | ||
import { readStore, saveStore } from './store-local-storage'; | ||
export const UPDATE_STORY_MSG = 'component_controls_update_story'; | ||
|
||
export interface MessageType { | ||
story?: Story; | ||
} | ||
|
||
export class BroadcastStore implements Store { | ||
config: RunConfiguration | undefined; | ||
docs: Documents = {}; | ||
stories: StoreStories = {}; | ||
components: StoreComponents = {}; | ||
packages: StorePackages = {}; | ||
observers: StoreObserver[]; | ||
channel: BroadcastChannel; | ||
|
||
constructor(store: Store) { | ||
this.assignStore(store); | ||
this.observers = []; | ||
this.channel = new BroadcastChannel<MessageType>(UPDATE_STORY_MSG, { | ||
type: 'localstorage', | ||
}); | ||
this.channel.onmessage = ({ story }: MessageType) => { | ||
this.readData(); | ||
this.notifyObservers(story); | ||
}; | ||
} | ||
|
||
/** | ||
* add observer callback function | ||
*/ | ||
addObserver = (observer: StoreObserver) => this.observers.push(observer); | ||
|
||
/** | ||
* remove installed observer callback function | ||
*/ | ||
removeObserver = (observer: StoreObserver) => | ||
(this.observers = this.observers.filter(o => o !== observer)); | ||
|
||
notifyObservers = (story?: Story) => { | ||
if (this.observers.length > 0) { | ||
this.observers.forEach(observer => observer(story)); | ||
} | ||
}; | ||
saveStore = (story?: Story) => { | ||
saveStore(this); | ||
this.broadcastMessage(story); | ||
}; | ||
private assignStore = (store: Store) => { | ||
this.config = store.config; | ||
this.docs = store.docs; | ||
this.stories = store.stories; | ||
this.components = store.components; | ||
this.packages = store.packages; | ||
}; | ||
readData = () => { | ||
const store = readStore(this.stories); | ||
this.assignStore(store); | ||
}; | ||
|
||
private broadcastMessage = (story?: Story) => { | ||
const message: MessageType = { | ||
story, | ||
}; | ||
this.channel.postMessage(message); | ||
}; | ||
/** | ||
* modify story properties, for example controls values. | ||
* will notify all installed store observers of the changed story. | ||
*/ | ||
updateStory = (story: Story): void => { | ||
if (story) { | ||
if (story.id) { | ||
this.stories = { | ||
...this.stories, | ||
[story.id]: story, | ||
}; | ||
} | ||
} | ||
this.saveStore(story); | ||
this.notifyObservers(); | ||
}; | ||
} |
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
Oops, something went wrong.