Skip to content

Commit

Permalink
[review-changes] Squashed commit with initial spike
Browse files Browse the repository at this point in the history
  • Loading branch information
judofyr authored and rexxars committed Oct 6, 2020
1 parent 7a6b969 commit 51e2b08
Show file tree
Hide file tree
Showing 144 changed files with 5,286 additions and 1,581 deletions.
22 changes: 11 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ const IGNORED_PACKAGES = [
'packages/create-sanity',
'packages/storybook',
'packages/sanity',
/packages\/.*-studio/
/packages\/.*-studio/,
]

const PACKAGE_PATHS = getPackagePaths().filter(pkgPath =>
IGNORED_PACKAGES.every(pattern =>
const PACKAGE_PATHS = getPackagePaths().filter((pkgPath) =>
IGNORED_PACKAGES.every((pattern) =>
typeof pattern === 'string' ? pattern !== pkgPath : !pattern.test(pkgPath)
)
)
Expand All @@ -53,7 +53,7 @@ const TASK_INFO = {
dts: {title: 'TypeScript (d.ts)', color: chalk.blueBright},
assets: {title: 'Assets (copy)', color: chalk.greenBright},
watch: {title: 'Watch', color: chalk.cyanBright},
_unknown: {title: 'Unknown', color: chalk.white}
_unknown: {title: 'Unknown', color: chalk.white},
}

const compileTaskName = (taskType, packagePath, extra = '') => {
Expand All @@ -69,7 +69,7 @@ function buildJavaScript(packageDir) {
.pipe(
changed(DEST_DIR, {
cwd: packageDir,
transformPath: orgPath => orgPath.replace(/\.tsx?$/, '.js')
transformPath: (orgPath) => orgPath.replace(/\.tsx?$/, '.js'),
})
)
.pipe(babel())
Expand Down Expand Up @@ -117,20 +117,20 @@ function watchPackage(name, packageDir, task) {
return withDisplayName(name, () => watch([`${SRC_DIR}/**/*`], {cwd: packageDir}, task))
}

const isTSProject = packageDir => {
const isTSProject = (packageDir) => {
const tsConfigPath = path.join(packageDir, 'tsconfig.json')
return fs.existsSync(tsConfigPath)
}

// We the list of packages ordered by topology to make sure we compile in the correct order
const ORDERED_PACKAGES = getPackagesOrderedByTopology().map(pkgName => `packages/${pkgName}`)
const ORDERED_PACKAGES = getPackagesOrderedByTopology().map((pkgName) => `packages/${pkgName}`)

const TS_PROJECTS = ORDERED_PACKAGES.filter(isTSProject)

const buildTS = series(TS_PROJECTS.map(buildTypeScript))

const watchTS = parallel(
flatten(TS_PROJECTS).map(packageDir =>
flatten(TS_PROJECTS).map((packageDir) =>
watchPackage(
compileTaskName('watch', packageDir, 'TS'),
packageDir,
Expand All @@ -141,7 +141,7 @@ const watchTS = parallel(

const buildJSAndAssets = parallel(PACKAGE_PATHS.map(buildPackage))
const watchJSAndAssets = parallel(
PACKAGE_PATHS.map(packageDir =>
PACKAGE_PATHS.map((packageDir) =>
watchPackage(
compileTaskName('watch', packageDir, 'JS/Assets'),
packageDir,
Expand Down Expand Up @@ -176,11 +176,11 @@ function studioTask(name, port) {
['blog-studio', '3336'],
['ecommerce-studio', '3337'],
['clean-studio', '3338'],
['storybook', '9002']
['storybook', '9002'],
].forEach(([name, port]) => {
exports[name] = studioTask(name, port)
})

exports.build = parallel(buildJSAndAssets, buildTS)
exports.watch = series(parallel(buildJSAndAssets, buildTS), parallel(watchJSAndAssets, watchTS))
exports.clean = () => del(PACKAGE_PATHS.map(pth => path.join(pth, DEST_DIR)))
exports.clean = () => del(PACKAGE_PATHS.map((pth) => path.join(pth, DEST_DIR)))
1 change: 1 addition & 0 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"packages/eslint-config-sanity",
"packages/example-studio",
"packages/groq",
"packages/mendoza",
"packages/movies-studio",
"packages/sanity",
"packages/storybook",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"private": true,
"scripts": {
"bootstrap": "lerna bootstrap && npm run package-yarn",
"dev": "npm start",
"rebuild": "gulp build",
"build": "gulp clean && gulp build && npm run package",
"clean": "gulp clean && lerna clean",
Expand Down
24 changes: 24 additions & 0 deletions packages/@sanity/base/sanity.json
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,18 @@
"name": "part:@sanity/base/public-icon",
"description": "Public icon"
},
{
"name": "part:@sanity/base/publish-icon",
"description": "Publish icon"
},
{
"name": "part:@sanity/base/truncate-icon",
"description": "Truncate icon"
},
{
"name": "part:@sanity/base/unpublish-icon",
"description": "Unpublish icon"
},
{
"name": "part:@sanity/base/search-icon",
"description": "Search icon"
Expand Down Expand Up @@ -815,6 +827,18 @@
"implements": "part:@sanity/base/public-icon",
"path": "components/icons/Public.js"
},
{
"implements": "part:@sanity/base/publish-icon",
"path": "components/icons/Publish.js"
},
{
"implements": "part:@sanity/base/truncate-icon",
"path": "components/icons/Truncate.js"
},
{
"implements": "part:@sanity/base/unpublish-icon",
"path": "components/icons/Unpublish.js"
},
{
"implements": "part:@sanity/base/circle-check-icon",
"path": "components/icons/CheckCircle.js"
Expand Down
4 changes: 4 additions & 0 deletions packages/@sanity/base/src/@types/parts.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
declare module 'part:*'
declare module 'all:part:*'
declare module 'part:@sanity/base/client' {
const client: import('@sanity/client').SanityClient
export default client
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
CommitFunction,
CommittedEvent,
DocumentMutationEvent,
DocumentRemoteMutationEvent,
DocumentRebaseEvent,
SnapshotEvent
SnapshotEvent,
} from './types'
import {ListenerEvent} from '../getPairListener'
import {Mutation} from '../types'
Expand All @@ -14,6 +15,7 @@ export type BufferedDocumentEvent =
| SnapshotEvent
| DocumentRebaseEvent
| DocumentMutationEvent
| DocumentRemoteMutationEvent
| CommittedEvent

const prepare = id => document => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
DocumentMutationEvent,
DocumentRebaseEvent,
MutationPayload,
SnapshotEvent
SnapshotEvent,
DocumentRemoteMutationEvent
} from './types'

import {ListenerEvent, MutationEvent} from '../getPairListener'
Expand Down Expand Up @@ -50,6 +51,7 @@ const getUpdatedSnapshot = (bufferedDocument: BufferedDocument) => {

return {
...LOCAL,
_type: (HEAD || LOCAL)._type,
_rev: (HEAD || LOCAL)._rev,
_updatedAt: new Date().toISOString()
}
Expand Down Expand Up @@ -78,6 +80,9 @@ export const createObservableBufferedDocument = (
// a stream of rebase events emitted from the mutator
const rebase$ = new Subject<DocumentRebaseEvent>()

// a stream of remote mutations with effetcs
const effects$ = new Subject<DocumentRemoteMutationEvent>();

const createInitialBufferedDocument = initialSnapshot => {
const bufferedDocument = new BufferedDocument(initialSnapshot)
bufferedDocument.onMutation = ({mutation, remote}) => {
Expand All @@ -92,6 +97,16 @@ export const createObservableBufferedDocument = (
})
}

bufferedDocument.onRemoteMutation = (mutation) => {
effects$.next({
type: 'remoteMutation',
transactionId: mutation.transactionId,
timestamp: mutation.timestamp,
author: mutation.identity,
effects: mutation.effects
})
}

bufferedDocument.onRebase = (edge, remoteMutations, localMutations) => {
rebase$.next({type: 'rebase', document: edge, remoteMutations, localMutations})
}
Expand Down Expand Up @@ -122,7 +137,7 @@ export const createObservableBufferedDocument = (
}

const currentBufferedDocument$ = listenerEvent$.pipe(
scan((bufferedDocument, listenerEvent): BufferedDocument => {
scan((bufferedDocument: BufferedDocument | null, listenerEvent) => {
// consider renaming 'snapshot' to initial/welcome
if (listenerEvent.type === 'snapshot') {
if (bufferedDocument) {
Expand Down Expand Up @@ -197,7 +212,7 @@ export const createObservableBufferedDocument = (
).pipe(map(toSnapshotEvent), publishReplay(1), refCount())

return {
updates$: merge(snapshot$, actionHandler$, mutations$, rebase$),
updates$: merge(snapshot$, actionHandler$, mutations$, rebase$, effects$),
consistency$: consistency$.pipe(distinctUntilChanged(), publishReplay(1), refCount()),
addMutation,
addMutations,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {SanityDocument} from '../types'
import {Mutation} from '@sanity/mutator'
import {SanityDocument} from '../types'

export type CommitFunction = (mutation: Mutation['params']) => Promise<unknown>

Expand All @@ -26,6 +26,17 @@ export interface CommittedEvent {
type: 'committed'
}

export interface DocumentRemoteMutationEvent {
type: 'remoteMutation'
transactionId: string
author: string
timestamp: Date
effects: {
apply: unknown
revert: unknown
}
}

// HTTP API Mutation payloads
// Note: this is *not* the same as the Mutation helper class exported by @sanity/mutator
export interface MutationPayload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ export interface EditStateFor {
export const editState = memoize(
(idPair: IdPair, typeName: string): Observable<EditStateFor> => {
return operationArgs(idPair, typeName).pipe(
switchMap(({draft, published}) => combineLatest([draft.snapshots$, published.snapshots$])),
map(([draftSnapshot, publishedSnapshot]) => ({
map(({snapshots}) => ({
id: idPair.publishedId,
type: typeName,
draft: draftSnapshot,
published: publishedSnapshot,
draft: snapshots.draft,
published: snapshots.published,
liveEdit: isLiveEditEnabled(typeName)
})),
publishReplay(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const duplicate = {
const source = snapshots.draft || snapshots.published
return client.create({
...omit(source, omitProps),
_id: isLiveEditEnabled(typeName) ? dupeId : getDraftId(dupeId)
_id: isLiveEditEnabled(typeName) ? dupeId : getDraftId(dupeId),
_type: source._type
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const publish = {
// before being created, we don't want to overwrite if, instead we want to yield an error
tx.create({
...omit(snapshots.draft, '_updatedAt'),
_id: idPair.publishedId
_id: idPair.publishedId,
_type: snapshots.draft._type
})
} else {
// If it exists already, we only want to update it if the revision on the remote server
Expand All @@ -33,7 +34,8 @@ export const publish = {
ifRevisionID: snapshots.published._rev
}).createOrReplace({
...omit(snapshots.draft, '_updatedAt'),
_id: idPair.publishedId
_id: idPair.publishedId,
_type: snapshots.draft._type
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const unpublish = {
if (snapshots.published) {
tx = tx.createIfNotExists({
...omit(snapshots.published, '_updatedAt'),
_id: idPair.draftId
_id: idPair.draftId,
_type: snapshots.published._type
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {merge, Observable} from 'rxjs'
import {filter, publishReplay, refCount, switchMap} from 'rxjs/operators'
import {DocumentRemoteMutationEvent} from '../buffered-doc/types'
import {IdPair} from '../types'
import {memoize} from '../utils/createMemoizer'
import {memoizedPair} from './memoizedPair'
import {DocumentVersion} from './checkoutPair'

export type RemoteMutationWithVersion = DocumentRemoteMutationEvent & {
version: 'published' | 'draft'
}

function withRemoteMutation(version: DocumentVersion) {
return version.events.pipe(
filter((ev): ev is RemoteMutationWithVersion => ev.type === 'remoteMutation'),
publishReplay(1),
refCount()
)
}

export const remoteMutations = memoize(
(idPair: IdPair): Observable<RemoteMutationWithVersion> => {
return memoizedPair(idPair).pipe(
switchMap(({published, draft}) =>
merge(withRemoteMutation(published), withRemoteMutation(draft))
),
publishReplay(1),
refCount()
)
},
idPair => idPair.publishedId
)
18 changes: 9 additions & 9 deletions packages/@sanity/base/src/datastores/document/getPairListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function getPairListener(client: SanityClient, idPair: IdPair) {
publishedId,
draftId
},
{includeResult: false, events: ['welcome', 'mutation', 'reconnect']}
{includeResult: false, events: ['welcome', 'mutation', 'reconnect'], effectFormat: 'mendoza'}
) as Observable<WelcomeEvent | MutationEvent | ReconnectEvent>
).pipe(
concatMap(event =>
Expand All @@ -51,14 +51,14 @@ export function getPairListener(client: SanityClient, idPair: IdPair) {
)

function fetchInitialDocumentSnapshots({publishedId, draftId}): Observable<Snapshots> {
return (client.observable.getDocuments([draftId, publishedId]) as Observable<
[SanityDocument, SanityDocument]
>).pipe(
map(([draft, published]) => ({
draft,
published
}))
)
return client.observable
.getDocuments<SanityDocument>([draftId, publishedId])
.pipe(
map(([draft, published]) => ({
draft,
published
}))
)
}
}

Expand Down

0 comments on commit 51e2b08

Please sign in to comment.