Skip to content

Commit 8c3ef2b

Browse files
committed
feat(overmind-svelte): add svelte plugin
1 parent c449e0e commit 8c3ef2b

File tree

7 files changed

+170
-0
lines changed

7 files changed

+170
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"react-icons": "^3.5.0",
5858
"react-split-pane": "0.1.87",
5959
"styled-components": "3.3.3",
60+
"svelte": "^3.20.1",
6061
"tslib": "1.10.0",
6162
"vscode": "^1.1.36",
6263
"vue": "2.6.10",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src
2+
jest.config.js
3+
tsconfig.json
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# overmind-vue
2+
3+
[https://overmindjs.org](https://overmindjs.org)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
collectCoverage: true,
3+
collectCoverageFrom: ['src/**/*.{t,j}s?(x)', '!src/**/*.d.ts'],
4+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest',
7+
},
8+
testRegex: '\\.test\\.tsx?$',
9+
testPathIgnorePatterns: [
10+
'/dist/',
11+
'/es/',
12+
'/lib/',
13+
'<rootDir>/node_modules/',
14+
],
15+
transformIgnorePatterns: ['<rootDir>/node_modules/'],
16+
coveragePathIgnorePatterns: ['<rootDir>/node_modules/'],
17+
haste: {
18+
// This option is needed or else globbing ignores <rootDir>/node_modules.
19+
providesModuleNodeModules: ['overmind-vue'],
20+
},
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "overmind-vue",
3+
"version": "1.0.0-alpha1",
4+
"description": "Functional actions",
5+
"author": "Christian Alfoni <[email protected]>",
6+
"license": "MIT",
7+
"repository": "git+https://github.com/cerebral/overmind.git",
8+
"main": "lib/index.js",
9+
"module": "es/index.js",
10+
"types": "lib/index.d.ts",
11+
"scripts": {
12+
"build": "npm run build:lib & npm run build:es",
13+
"build:lib": "tsc --outDir lib --module commonjs",
14+
"build:es": "tsc --outDir es --module es2015",
15+
"clean": "rimraf es lib coverage",
16+
"typecheck": "tsc --noEmit",
17+
"test": "jest --runInBand",
18+
"test:watch": "jest --watch --updateSnapshot --coverage false",
19+
"prebuild": "npm run clean",
20+
"postbuild": "rimraf {lib,es}/**/__tests__",
21+
"posttest": "npm run typecheck"
22+
},
23+
"keywords": [
24+
"state",
25+
"sideeffects",
26+
"app",
27+
"framework"
28+
],
29+
"files": [
30+
"lib",
31+
"es",
32+
"react"
33+
],
34+
"dependencies": {
35+
"overmind": "next",
36+
"svelte": "^3.20.1"
37+
},
38+
"devDependencies": {
39+
"@types/node": "^12.11.6",
40+
"tslib": "^1.10.0"
41+
}
42+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { EventType } from 'overmind'
2+
import { onMount, afterUpdate, onDestroy } from 'svelte'
3+
4+
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
5+
6+
let nextComponentId = 0
7+
8+
export function createMixin (overmind) {
9+
const componentId = nextComponentId++
10+
let nextComponentInstanceId = 0
11+
let currentFlushId = 0
12+
13+
const subscribe = listener => {
14+
const tree = overmind.proxyStateTree.getTrackStateTree()
15+
const componentInstanceId = nextComponentInstanceId++
16+
let isUpdating = false
17+
18+
tree.track((mutations, paths, flushId) => {
19+
currentFlushId = flushId
20+
isUpdating = true
21+
listener(tree)
22+
})
23+
24+
listener(tree)
25+
26+
if (IS_PRODUCTION) {
27+
afterUpdate(() => {
28+
tree.stopTracking()
29+
isUpdating = false
30+
})
31+
} else {
32+
onMount(() => {
33+
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
34+
componentId,
35+
componentInstanceId,
36+
name: '',
37+
paths: Array.from(tree.pathDependencies)
38+
})
39+
})
40+
41+
afterUpdate(() => {
42+
tree.stopTracking()
43+
if (isUpdating) {
44+
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
45+
componentId,
46+
componentInstanceId,
47+
name: '',
48+
flushId: currentFlushId,
49+
paths: Array.from(tree.pathDependencies)
50+
})
51+
}
52+
isUpdating = false
53+
})
54+
}
55+
56+
return () => {
57+
tree.stopTracking()
58+
overmind.proxyStateTree.disposeTree(tree)
59+
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
60+
componentId,
61+
componentInstanceId: componentInstanceId,
62+
name: ''
63+
})
64+
}
65+
}
66+
67+
const reaction = (...args) => {
68+
const dispose = overmind.reaction(...args)
69+
70+
onDestroy(() => {
71+
dispose()
72+
})
73+
}
74+
75+
return {
76+
subscribe,
77+
state: overmind.state,
78+
actions: overmind.actions,
79+
effects: overmind.effects,
80+
addMutationListener: overmind.addMutationListener,
81+
reaction: reaction
82+
}
83+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs",
5+
"rootDir": "src",
6+
"outDir": ".code",
7+
"newLine": "LF",
8+
"lib": ["dom", "es2017"],
9+
"moduleResolution": "node",
10+
"importHelpers": true,
11+
"declaration": true,
12+
"pretty": true,
13+
"sourceMap": true,
14+
"inlineSources": true
15+
},
16+
"exclude": ["node_modules", "dist", "es", "lib", "src/**/*.test.ts"]
17+
}

0 commit comments

Comments
 (0)