Skip to content

Commit 761f2fb

Browse files
committed
Remove DOCUMENTS_LOADED event
1 parent d0f0281 commit 761f2fb

File tree

5 files changed

+42
-50
lines changed

5 files changed

+42
-50
lines changed

pages/notepad.vue

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
<template>
2-
<EditorPage v-if="daily" :docId="daily.id" />
3-
</template>
4-
51
<script>
62
import moment from 'moment'
7-
import EditorPage from '#root/pages/docs/[docId]/index.vue'
8-
import Doc from '#root/src/models/doc'
9-
import { DOCUMENTS_LOADED, EDIT_DOCUMENT } from '#root/src/store/actions'
3+
import { computed, onMounted, watch } from 'vue'
4+
import EditorPage from '/pages/docs/[docId]/index.vue'
5+
import Doc from '/src/models/doc'
6+
import { EDIT_DOCUMENT } from '/src/store/actions'
107
118
export default {
129
components: {
1310
EditorPage,
1411
},
15-
computed: {
16-
daily() {
17-
return this.$store.getters.daily
18-
},
19-
},
20-
methods: {
21-
async buildTemplate() {
12+
setup() {
13+
const { store } = useVuex()
14+
const daily = computed(() => store.getters.daily)
15+
16+
const buildTemplate = async () => {
2217
try {
23-
const quotes = (await import('#root/src/data/quotes.json')).default
18+
const quotes = (await import('/src/data/quotes.json')).default
2419
const quote = quotes[Math.floor(Math.random() * quotes.length)]
2520
2621
return `#daily\n\n# ${moment().format('dddd, MMMM Do, YYYY')}\n\n> ${quote.text}\n> ${quote.author || 'Unknown'}\n\n`
@@ -29,37 +24,46 @@ export default {
2924
3025
return `#daily\n\n# ${moment().format('dddd, MMMM Do, YYYY')}\n\n`
3126
}
32-
},
33-
async checkDaily() {
27+
}
28+
29+
const checkDaily = async () => {
3430
let cutoff = moment().startOf('day').add(3, 'hours')
3531
3632
if (moment() < cutoff) {
3733
cutoff = cutoff.subtract(1, 'day')
3834
}
3935
40-
if (!this.daily || this.daily.createdAt < cutoff) {
41-
const template = await this.buildTemplate()
36+
if (!daily.value || daily.value.createdAt < cutoff) {
37+
const template = await buildTemplate()
4238
const doc = new Doc({ text: template, daily: true })
4339
44-
this.$store.commit(EDIT_DOCUMENT, doc)
40+
store.commit(EDIT_DOCUMENT, doc)
4541
}
46-
},
47-
},
48-
beforeMount() {
49-
if (this.$store.state.documents.loaded) {
50-
this.checkDaily()
51-
} else {
52-
this.$store.subscribe(({ type }, _state) => {
53-
if (type === DOCUMENTS_LOADED) {
54-
this.checkDaily()
55-
}
56-
})
5742
}
58-
},
59-
setup() {
43+
6044
useHead({
6145
title: 'Notepad',
6246
})
47+
48+
onMounted(() => {
49+
if (store.state.documents.loaded) {
50+
checkDaily()
51+
}
52+
})
53+
54+
watch(() => store.state.documents.loaded, (loaded) => {
55+
if (loaded) {
56+
checkDaily()
57+
}
58+
})
59+
60+
return {
61+
daily,
62+
}
6363
},
6464
}
6565
</script>
66+
67+
<template>
68+
<EditorPage v-if="daily" :doc-id="daily.id" />
69+
</template>

src/store/actions.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export const BLUR_EDITOR = 'BLUR_EDITOR'
44
export const DEACTIVATE_CONTEXT = 'DEACTIVATE_CONTEXT'
55
export const DISCARD_DOCUMENT = 'DISCARD_DOCUMENT'
66
export const DUPLICATE_DOCUMENT = 'DUPLICATE_DOCUMENT'
7-
export const DOCUMENTS_LOADED = 'DOCUMENTS_LOADED'
87
export const EDIT_DOCUMENT = 'EDIT_DOCUMENT'
98
export const FOCUS_EDITOR = 'FOCUS_EDITOR'
109
export const LOAD_DOCUMENT = 'LOAD_DOCUMENT'
@@ -32,7 +31,6 @@ export default {
3231
DEACTIVATE_CONTEXT,
3332
DISCARD_DOCUMENT,
3433
DUPLICATE_DOCUMENT,
35-
DOCUMENTS_LOADED,
3634
EDIT_DOCUMENT,
3735
FOCUS_EDITOR,
3836
LOAD_DOCUMENT,

src/store/modules/documents.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useRecentDocs } from '#root/src/stores/useRecentDocs'
33

44
import {
55
DISCARD_DOCUMENT,
6-
DOCUMENTS_LOADED,
76
DUPLICATE_DOCUMENT,
87
EDIT_DOCUMENT,
98
LOAD_DOCUMENT,
@@ -67,9 +66,6 @@ export default {
6766
[DISCARD_DOCUMENT](state, { id }) {
6867
findDoc(state, id).discard()
6968
},
70-
[DOCUMENTS_LOADED](state) {
71-
state.loaded = true
72-
},
7369
[EDIT_DOCUMENT](state, doc) {
7470
const found = findDoc(state, doc.id)
7571

@@ -111,9 +107,6 @@ export default {
111107
async [DISCARD_DOCUMENT](context, doc) {
112108
context.commit(DISCARD_DOCUMENT, doc)
113109
},
114-
async [DOCUMENTS_LOADED](context) {
115-
context.commit(DOCUMENTS_LOADED)
116-
},
117110
async [DUPLICATE_DOCUMENT](context, { id }) {
118111
const newDoc = findDoc(context.state, id).duplicate()
119112

@@ -128,9 +121,11 @@ export default {
128121
context.commit(LOAD_DOCUMENT, doc)
129122
},
130123
async [LOAD_DOCUMENTS](context, docs) {
131-
return Promise.all(
124+
await Promise.all(
132125
docs.map(doc => context.dispatch(LOAD_DOCUMENT, doc)),
133126
)
127+
128+
context.state.loaded = true
134129
},
135130
async [MERGE_DOCUMENT](context, doc) {
136131
context.commit(MERGE_DOCUMENT, doc)

src/store/plugins/caching/documents.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { type Doc, pack, unpack } from '/src/models/doc'
55
import {
66
ADD_DOCUMENT,
77
DISCARD_DOCUMENT,
8-
DOCUMENTS_LOADED,
98
EDIT_DOCUMENT,
109
LOAD_DOCUMENTS,
1110
MERGE_DOCUMENT,
@@ -63,7 +62,6 @@ export const loadDocs = async (store: Store<any>) => {
6362
)
6463

6564
await store.dispatch(LOAD_DOCUMENTS, docs)
66-
await store.dispatch(DOCUMENTS_LOADED)
6765
}
6866

6967
export default docsPlugin

src/store/plugins/packages.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import PackageManager from '#root/src/packages/manager'
33
import {
44
ADD_DOCUMENT,
55
DISCARD_DOCUMENT,
6-
DOCUMENTS_LOADED,
76
EDIT_DOCUMENT,
8-
LOAD_DOCUMENTS,
97
RESTORE_DOCUMENT,
108
TOUCH_DOCUMENT,
119
} from '#root/src/store/actions'
@@ -18,7 +16,6 @@ export default (store) => {
1816
case EDIT_DOCUMENT:
1917
case RESTORE_DOCUMENT:
2018
case TOUCH_DOCUMENT:
21-
case DOCUMENTS_LOADED:
2219
// find the affected document when possible
2320
const doc = state.documents.all.find(d => d.id === id)
2421

@@ -29,5 +26,5 @@ export default (store) => {
2926
default:
3027
break
3128
}
32-
});
33-
};
29+
})
30+
}

0 commit comments

Comments
 (0)